<template>
	<view class="quesiton-page-qx" :style="{'background-image':`url(${getPageBg})`}">
		<navigationBar :emitBackEvent="true" @backEvent="$emit('backHandle')" :my-property="navigationBarDataInfo" />

		<view class="main-wrapper">
			<view class="row-1">
				<view class="progress-bar">
					<view class="text">{{current}}</view>
					<view class="text">/{{max}}</view>
				</view>
				<view class="clock-wrapper">
					<image class="clock-icon" :src="staticImgs.clockImg" mode="widthFix"></image>
					<view class="clock-text">
						{{countTimeStr}}
					</view>
				</view>
			</view>
			<view class="question-box">
				{{question || ''}}
			</view>
			<view v-if="current === 1">
				<select-sex-mai-lun @selectMaiLunSex="selectMaiLunSex" :mai-lun-sex="maiLunSex" />
			</view>
			<template v-else>
				<view style="margin-top: 60rpx;">
					<view v-for="(item,index) in optnList" :key="index" :class="[(value-1)==index?'ml-item-select':'ml-item','flex-cc']" @click="changeSlideVal(index)">
						{{item.optnContent}}
					</view>
				</view>
			
				<view class="btn-wrapper" :style="{'marginTop':'80rpx'}">
					<view class="next-btn">
						<image @click="preBtnHandle" class="img" :src="staticImgs.preImg" mode="widthFix" />
					</view>

					<view class="next-btn">
						<image @click="nextBtnHandle" class="img" :src="staticImgs.submitImg" mode="widthFix"
							v-show="current === max" />
						<image @click="nextBtnHandle" class="img" :src="staticImgs.nextImg" mode="widthFix"
							v-show="current != max" />
					</view>
				</view>
			</template>
		</view>

	</view>
</template>

<script>
	import navigationBar from "@/components/navigationBar/navigationBar.vue";
	import lxProgressBar from '@/subcontractorD/components/lx-progress-bar/components/lx-progress-bar/lx-progress-bar.vue'
	import haoSlider from '@/components/hao-slider.vue'
	import SelectSexMaiLun from '@/subcontractorD/components/selectSexMaiLun/selectSexMaiLun.vue'
	import {
		updateUserInfo
	} from '@/common/api/user_api'
	import {
		mapGetters
	} from 'vuex'

	var app = getApp();
	let countTimer;
	let clockTimer;
	export default {
		components: {
			SelectSexMaiLun,
			navigationBar,
			lxProgressBar,
			haoSlider
		},
		props: {
			percent: {
				type: Number,
				default: 0
			},
			max: {
				type: Number,
				default: 0
			},
			current: {
				type: Number,
				default: 0
			},
			countTime: {
				type: String,
				default: ''
			},
			optnList: {
				type: Array,
				default: () => []
			},
			question: {
				type: String,
				default: ''
			},
			mbtiType: {
				type: String,
				default: 'job-mbti'
			}
		},
		data() {
			return {
				navigationBarDataInfo: {
					"bg_color": "transparent",
					"color": "#fff",
					"flag": 4,
					"name": "",
					"hasEntity": true,
				},
				value: -1,
				staticImgs: {
					nextImg: this.$imgWxkjUrl + '/product/question/v2-btn-right_new_new.png',
					preImg: this.$imgWxkjUrl + '/product/question/v2-btn-left_new_new.png',
					leftLabel: this.$imgWxkjUrl + '/product/question/v2-dialog-l-bg.png',
					rightLabel: this.$imgWxkjUrl + '/product/question/v2-dialog-r-bg.png',
					actDot: this.$imgWxkjUrl + '/product/question/v2-dot-act.png',
					defDot: this.$imgWxkjUrl + '/product/question/v2-dot-def.png',
					clockImg: this.$imgWxkjUrl + '/product/question/time_ic_new.png',
					submitImg: this.$imgWxkjUrl + '/product/question/submit_new.png'
				},
				topH: 0,
				// percentNum:40,
				legendNum: 0,
				reRenderLegend: false,
				selectIndex: -1,
				countTimeStr: '',
				startDateTime: 0,
				hasOverMaxOptCon: false,
				firstTap: false,
				moreAnswer: [],
				lastSelectIndex: null,
				gender: null,
				reRenderSlider: false,
				maiLunSex: 0
			}
		},
		computed: {
			tipInfo() {
				return '提示：请选择你感受的程度'
			},
			getPageBg() {
				return this.$imgWxkjUrl + '/product/sex/ml_do_bg.png'
				// if (this.current === 1) {
				// } else if (this.current < 6) {
				// 	return this.$imgWxkjUrl + '/product/question/ml_do_1.png'
				// } else if (this.current < 12) {
				// 	return this.$imgWxkjUrl + '/product/question/ml_do_2.png'
				// } else if (this.current < 18) {
				// 	return this.$imgWxkjUrl + '/product/question/ml_do_3.png'
				// } else if (this.current < 24) {
				// 	return this.$imgWxkjUrl + '/product/question/ml_do_4.png'
				// } else {
				// 	return this.$imgWxkjUrl + '/product/question/ml_do_5.png'
				// }
			},
			...mapGetters('user', ['uGender'])
		},
		watch: {
			current(val, oldVal) {
				const _index = this.optnList.findIndex(item => item.select == '1')
				console.log(_index, '====_index')
				this.value = _index > -1 ? _index + 1 : -1
				// console.log(this.optnList,'----optnList')

				this.reRenderSlider = true
				this.$nextTick(() => {
					this.reRenderSlider = false
				})
			}
		},
		mounted() {
			this.maiLunSex = this.uGender || 0
			this.topH = app.globalData.topHeight * 2

			this.startDateTime = this.$dayjs()
			clockTimer = setInterval(() => {
				// 00:00 这种格式
				this.countTimeStr = this.$dayjs().subtract(this.startDateTime).format('mm:ss')
			}, 1000)
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: '#000000'
			})
		},
		beforeDestroy() {
			uni.setNavigationBarColor({
				frontColor: '#000000',
				backgroundColor: '#ffffff'
			})
			clearInterval(clockTimer)
		},
		methods: {
			changeSlideVal(val) {
				if (this.$fangBaoClick(60)) return
				this.value = val + 1
				console.log(this.value, '-------this.value')
				this.changeFirstTap()
				setTimeout(() => {
					this.nextBtnHandle()
				}, 60)
			},
			changeGender(str) {
				this.gender = str
			},
			changeLastSelectIndex(index) {
				this.lastSelectIndex = index
			},
			changeFirstTap() {
				this.firstTap = false
			},
			selectAnswer(item, index, finalData) {
				this.selectIndex = index
				this.current === this.max ?
					this.$emit('updateSelectSclQuestion', index, undefined, null) :
					this.$emit('updateSelectSclQuestion', index)
			},
			preBtnHandle() {
				this.$emit('prev')
			},
			nextBtnHandle() {
				if (this.value == -1) {
					this.$showToast('请选择你感受的程度')
					return
				}
				this.selectAnswer(this.optnList[this.value - 1], this.value - 1)
			},
			selectMaiLunSex(maiLunSex) {
				this.maiLunSex = maiLunSex
				this.selectIndex = maiLunSex - 1
				this.$emit('updateSelectSclQuestion', maiLunSex - 1)
				updateUserInfo({
					sex: maiLunSex
				}).then(res => {
					if (res.data.code == 200) {
						let userInfo = this.$store.getters['user/getUserInfo'];
						userInfo.sex = maiLunSex
						this.$store.commit('user/setUserInfo', userInfo)
					} else {
						this.$showToast(res.data.message)
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.ml-item {
		width: 600rpx;
		height: 100rpx;
		background: rgba(255, 255, 255, 0.65);
		border-radius: 10rpx;
		font-size: 40rpx;
		color: #191D53;
		margin: 0 auto;
		margin-bottom: 40rpx;
	}
	
	.ml-item-select {
		width: 600rpx;
		height: 100rpx;
		background: linear-gradient( 180deg, #C8B2FA 0%, #977BF3 100%);;
		border-radius: 10rpx;
		font-size: 40rpx;
		color: #FFFFFF;
		margin: 0 auto;
		margin-bottom: 40rpx;
	}

	.ml-lr-text {
		font-size: 36rpx;
		color: #191D53;
		line-height: 50rpx;
		padding: 0 43rpx;
		box-sizing: border-box;
	}

	.mli-select-y {
		width: 30rpx;
		height: 30rpx;
		background: #A8A4F3;
		border-radius: 45rpx;
	}

	.mli-select {
		width: 60rpx;
		height: 60rpx;
		background: #FFFFFF;
		border-radius: 45rpx;
		border: 2rpx solid #6F79ED;
		opacity: 0.61;
		margin: 0 auto;
	}

	@keyframes shake {
		0% {
			transform: translateX(0) translateY(0);
		}

		50% {
			transform: translateY(-10%);
		}

		100% {
			transform: translateX(0) translateY(0);
		}
	}

	.quesiton-page-qx {
		position: relative;
		width: 100%;
		min-height: 100vh;

		background-repeat: no-repeat;
		background-size: cover;
		background-position: center top;

		.main-wrapper>.row-1 {
			display: flex;
			justify-content: space-between;
			align-items: center;

			margin-top: 60rpx;
			margin-bottom: 26rpx;
			padding-left: 36rpx;
			padding-right: 36rpx;
		}

		.progress-bar {
			display: flex;
			align-items: center;

			// justify-content: space-between;
			.text {
				font-weight: bold;
				font-size: 30rpx;
				color: #191D53;
				line-height: 42rpx;
				font-style: normal;

				&:last-child {
					color: #191D53;
				}
			}
		}

		.clock-wrapper {
			display: flex;
			align-items: center;

			.clock-icon {
				width: 36rpx;
				height: 36rpx;
				margin-right: 10rpx;
			}

			.clock-text {
				font-size: 30rpx;
				color: #191D53;
				line-height: 42rpx;
				text-align: left;
				font-style: normal;
			}
		}


		.question-box {
			width: 100%;
			height: 150rpx;
			font-weight: bold;
			font-size: 50rpx;
			color: #191D53;
			line-height: 70rpx;
			padding: 0 30rpx;
			box-sizing: border-box;
			word-break: break-all;
			white-space: pre-wrap;
			text-align: justify;
		}

		.options-wrapper {
			display: flex;
			flex-wrap: wrap;

			justify-content: center;
			row-gap: 26rpx;
			column-gap: 20rpx;

			.opt-item {
				width: 330rpx;
				height: 76rpx;
				background: #191D53;
				border-radius: 38rpx;
				display: flex;
				align-items: center;
				box-sizing: border-box;
				padding-left: 24rpx;

				.img-wrapper {
					display: flex;
					align-items: center;
					margin-right: 10rpx;

					.img {
						width: 40rpx;
						height: 40rpx;
					}
				}

				.text {
					font-size: 28rpx;
					color: #333333;
					line-height: 40rpx;
					text-align: left;
					font-style: normal;
				}
			}


		}

		.opt-type-1 {
			display: flex;
			justify-content: space-between;
			// margin: 0 46rpx 70rpx 46rpx;

			.left-label,
			.right-label {
				width: 400rpx;
				height: 200rpx;
				box-sizing: border-box;

				font-size: 33rpx;
				color: #242974;
				line-height: 48rpx;

				font-style: normal;

				background-size: 100% 100%;
				background-repeat: no-repeat;

				view {
					// padding-top: 24rpx;
					height: 96rpx;
					display: flex;
					align-items: center;
					width: 170rpx;
					word-break: break-word;
					margin: auto;
				}
			}

			.left-label {
				text-align: center;

				display: flex;
				align-items: center;

				padding-left: 58rpx;
			}

			.right-label {
				text-align: center;

				display: flex;
				align-items: center;
				justify-content: flex-end;

				padding-right: 58rpx;
			}

			.opt-type-text {
				font-size: 26rpx;
				color: #FFFFFF;
				line-height: 37rpx;
				font-style: normal;
			}

			.l-tip {
				margin-right: 36rpx;

				display: flex;
				align-items: flex-end;
				padding-bottom: 4rpx;
			}

			.r-tip {
				margin-left: 36rpx;
				padding-top: 4rpx;
			}


			.over-max-opt-con-label {
				font-size: 34rpx;
				color: #FFFFFF;
				line-height: 40rpx;
				width: 200rpx;
				word-break: break-word;
				margin: auto;
				// padding-top: 12rpx !important;
				height: 96rpx;
				display: flex;
				align-items: center;
			}



		}

		.slider-wrapper {
			width: 650rpx;
			margin: 40rpx auto;
			padding: 0 10rpx;

			min-height: 40rpx;

		}

		.tip-text {
			margin-top: 16rpx;
			font-weight: bold;
			font-size: 24rpx;
			color: #191D53;
			// line-height: 33rpx;
			text-align: center;
			font-style: normal;

			margin-bottom: 150rpx;
		}

		.btn-wrapper {
			display: flex;
			align-items: center;
			justify-content: center;
			column-gap: 100rpx;
			position: absolute;
			left: 50%;
			transform: translateX(-50%);
			bottom: 185rpx;
		}

		.next-btn {
			// margin: auto;
			// margin-top: 150rpx;
			display: flex;
			justify-content: center;

			.img {
				width: 180rpx;
				height: 180rpx;
			}
		}


		.gender-choice-wrapper {
			display: flex;
			justify-content: center;

			column-gap: 100rpx;
			margin-top: 120rpx;

			.gender-item {
				display: flex;
				align-items: center;
			}

			.img {
				width: 110rpx;
				height: 110rpx;
			}
		}


	}
</style>